﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.IO;

namespace linq
{
    class PlayListsModel
    {
        #region PROPERTIES
        public string XmlLocation { get; set; }
        public Dictionary<string, PlayListModel> PlayListsDic { get; set; }
        #endregion

        #region CTORS
        public PlayListsModel(string oo)
        {
            this.PlayListsDic = new Dictionary<string, PlayListModel>();
            this.Load();
        }
        #endregion

        #region LOADER SAVER
        public void Load()
        {

           XDocument loaded = XDocument.Load("./playlists.xml");

           var q = loaded.Element("playlists").Elements().Select(Playlist => new
           {
                    Name = (string) Playlist.Attribute("name"),
                    Medias = Playlist.Elements("media").Select(Medias => new {
                        Path = (string)Medias.Attribute("path")
                    })
               });

               foreach (var PlayList in q)
               {
                   PlayListsDic[PlayList.Name] = new PlayListModel();
                   PlayListsDic[PlayList.Name].MediaDic = new Dictionary<string, MediaModel>();
                   foreach (var Media in PlayList.Medias)
                       PlayListsDic[PlayList.Name].MediaDic[Media.Path] = new MediaModel(PlayListsDic[PlayList.Name], Media.Path);
               }   
        }

        public void Save()
        {
            XElement xml;
                
            xml = new XElement("playlists", "");

            foreach (KeyValuePair<string, PlayListModel> playlistsIt in this.PlayListsDic)
            {
                XElement concatPlayList;
                concatPlayList = new XElement("playlist",
                     new XAttribute("name", playlistsIt.Key)
                          );
            
                foreach (KeyValuePair<string, MediaModel> MediaIt in playlistsIt.Value.MediaDic)
                {
                    XElement concatMedia = new XElement("media", new XAttribute("path", MediaIt.Value.Location));
                    concatPlayList.Add(concatMedia);
                }
            xml.Add(concatPlayList);
            }
  
            try
            {
                xml.Save("./playlists.xml");
            }
            catch (Exception e)
            { }
        }

        #endregion

        #region SEARCHER
        public void DirSearch(string sDir)
        {
            string sPattern = ".iso|.jpg";
            string[] arrFolder = { };
            try
            {
                arrFolder = Directory.GetDirectories(sDir);
            }
            catch { }

            foreach (string fo in arrFolder)
            {
                try {
                    foreach (string f in Directory.GetFiles(fo))
                     {
                         if (System.Text.RegularExpressions.Regex.IsMatch(f, sPattern))
                             Console.WriteLine(f);
                     }
                    DirSearch(fo);
                }
                catch { }
            }
        }
        #endregion
    }
}
